
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    "index_url": "behavior/behavior/index",
                    "add_url": "behavior/behavior/add",
                    "edit_url": "behavior/behavior/edit",
                    "del_url": "behavior/behavior/del",
                    "multi_url": "behavior/behavior/multi",
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'name', title: __('Name')},
                        /*{field: 'image.url', title: __('图片'), formatter: Controller.api.formatter.url, operate: false},*/
                        {field: 'score', title: __('分值'), operate: false},
                        {field: 'type_text', title: __('类型'), operate: false},
                        {
                            field: 'type',
                            title: __('类型'),
                            align: 'left',
                            searchList: $.getJSON('behavior/behavior/typeList'),
                            visible: false
                        },
                        {field: 'category_text', title: __('行为分类'), operate: false},
                        {
                            field: 'category_id',
                            title: __('行为分类'),
                            align: 'left',
                            searchList: $.getJSON('behavior/behavior/categoryList'),
                            visible: false
                        },
                        {
                            field: 'status',
                            title: __('是否启用'),
                            align: 'center',
                            formatter: Table.api.formatter.toggle
                        },
                        {
                            field: 'created_at',
                            title: __('Create time'),
                            sortable: true,
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange'
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                commonSearch: true,
                searchFormVisible: true,

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        api: {
            formatter: {
                url: function (value, row, index) {
                    return '<img style="max-width: 50px; max-height: 50px;" src="'+value+'">';
                },
            },
            bindevent: function () {

            }
        }
    };
    return Controller;
});
